﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class HD_ADD_SP : Form
    {
        public string idhd;
        public string ngayhd;
        public string idncc;

        public HD_ADD_SP(string idhd,string ngayhd,string idncc)
        {
            this.idhd = idhd;
            this.ngayhd = ngayhd;
            this.idncc = idncc;
            InitializeComponent();
        }

        private void HD_ADD_SP_Load(object sender, EventArgs e)
        {
            load_LoaiSanPham(idncc);
            load_SanPham(loaisanpham.SelectedValue.ToString());            
        }

        private void loaisanpham_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_SanPham(loaisanpham.SelectedValue.ToString());
        }

        private void load_SanPham(string loai)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");            
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from SANPHAM where IDloai = @loai";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@loai", loai);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds,"Table SanPham");
            DataTable dt = ds.Tables["Table SanPham"];

            tsp.DisplayMember = "tensanpham";
            tsp.ValueMember = "IDsanpham";
            tsp.DataSource = ds.Tables["Table SanPham"];          
        }

        private void load_LoaiSanPham(string ncc)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select NCC_CT.IDloai,tenloai from NCC_CT join LOAISANPHAM on NCC_CT.IDloai = LOAISANPHAM.IDloai where IDNCC = @id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", ncc);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table LoaiSanPham");
            DataTable dt = ds.Tables["Table LoaiSanPham"];

            loaisanpham.DisplayMember = "tenloai";
            loaisanpham.ValueMember = "IDloai";
            loaisanpham.DataSource = ds.Tables["Table LoaiSanPham"];
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "";
            if (slnhd.Text == "" || dgnhd.Text == "")
                label2.Text = "Xin nhập đầy đủ dữ liệu";
            else
            {
                    Config file = new Config(Application.StartupPath + "\\config.ini");
                    string connectionString = file.ReadValue("server", "conn");
                    //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
                    SqlConnection conn = new SqlConnection(connectionString);
                    string sql = "select * from HOPDONG_CT where IDhopdong = @idhd and ngayhopdong = @ngayhd and IDsanpham = @id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@idhd", idhd);
                cmd.Parameters.AddWithValue("@ngayhd", ngayhd);
                cmd.Parameters.AddWithValue("@id", tsp.SelectedValue);

                conn.Open();
                SqlDataReader r = cmd.ExecuteReader();
                if (r.Read())
                {
                    label2.Text = "Sản phẩm đã có trong hợp đồng";
                    r.Close();
                }
                else
                {
                    r.Close();
                        label2.Text = "Đã thêm dữ liệu thành công";
                        this.DialogResult = DialogResult.OK;

                        cmd.Parameters.Clear();
                        cmd.CommandText = "insert into HOPDONG_CT(IDhopdong,ngayhopdong,IDsanpham,slnhap,dongianhap) values(@idhd,@ngayhd,@id,@sln,@dgn)";
                        cmd.Parameters.AddWithValue("@idhd", idhd);
                        cmd.Parameters.AddWithValue("@ngayhd", ngayhd);
                        cmd.Parameters.AddWithValue("@id", tsp.SelectedValue);
                        cmd.Parameters.AddWithValue("@sln", slnhd.Text);
                        cmd.Parameters.AddWithValue("@dgn", dgnhd.Text);
                        cmd.ExecuteNonQuery();
                }
                conn.Close();
            }     
        }




    }
}
